<template>
  <el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm" :size="formSize" status-icon>
    <table>
      <tr>
        <td>
          <el-form-item label="耳号" prop="earNumber">
            <el-input v-model="ruleForm.earNumber" :disabled="true" />
          </el-form-item>
        </td>
        <td>
          <el-form-item label="妊检日期" prop="pregnancyTestDate">
            <el-date-picker v-model="ruleForm.pregnancyTestDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择时间" :size="size"  />
          </el-form-item>
        </td>
      </tr>

      <tr>
        <td>
          <el-form-item label="单据号" prop="documentNumber">
            <el-input v-model="ruleForm.documentNumber" :disabled="true" />
          </el-form-item>
        </td>
        <td>
          <el-form-item label="流水号" prop="serialNumber">
            <el-input v-model="ruleForm.serialNumber" :disabled="true" />
          </el-form-item>
        </td>
      </tr>

      <tr>
        <td>
          <el-form-item label="妊检方式" required>
            <el-select v-model="ruleForm.pregnancyTestTypeId" placeholder="请选择" clearable>
                <el-option v-for="pregnancytype in pregnancyTypeList" :key="pregnancytype.pregnancyTestTypeId" :label="pregnancytype.pregnancyTestTypeName" :value="pregnancytype.pregnancyTestTypeId" />
              </el-select>
          </el-form-item>
        </td>
        <td>
          <el-form-item label="妊检结果" required>
            <el-select v-model="ruleForm.pregnancyTestResultId" placeholder="请选择" clearable>
              <el-option v-for="pregnancyresult in pregnancyResultList" :key="pregnancyresult.pregnancyTestResultId" :label="pregnancyresult.pregnancyTestResultName" :value="pregnancyresult.pregnancyTestResultId" @click="resultCheck(item.pregnancyTestResultId,index)" />
            </el-select>
          </el-form-item>
        </td>
      </tr>

      <tr>
        <td>
          <el-form-item label="同胎数" required>
            <el-input v-model="ruleForm.parityNumber" />
          </el-form-item>
        </td>
        <td>
          <el-form-item label="负责人" required>
            <el-select v-model="ruleForm.principal" placeholder="请选择" clearable>
                    <el-option v-for="user in userList" :key="user.userId" :label="user.userMyName" :value="user.userMyName" />
                </el-select>
          </el-form-item>
        </td>
      </tr>

      <tr>
        <td>
          <el-form-item label="当前栋舍" prop="currentBuilding">
            <el-input v-model="ruleForm.currentBuilding" :disabled="true" />
          </el-form-item>
        </td>
        <td>
          <el-form-item label="当前栏位" prop="currentfield">
            <el-input v-model="ruleForm.currentfield" :disabled="true" />
          </el-form-item>
        </td>
      </tr>

      <tr>
        <td>
          <el-form-item label="转入栋舍" required>
            <el-select v-model="ruleForm.turnBuilding" placeholder="请选择" @change="handlefield()" clearable>
              <el-option v-for="building in bedroomList" :key="building.bedroomId" :label="building.bedroomName" :value="building.bedroomId" />
            </el-select>
          </el-form-item>
        </td>
        <td>
          <el-form-item label="转入栏位" required>
            <el-select v-model="ruleForm.turnColumn" placeholder="请选择" clearable>
                <el-option v-for="column in fieldList" :key="column.fieldId" :label="column.fieldName" :value="column.fieldId" />
              </el-select>
          </el-form-item>
        </td>
      </tr>

      <tr>
        <td colspan="2" align="center">
          <el-form-item>
            <el-button type="primary" @click="submitForm(ruleFormRef)">保存</el-button>
            <el-button @click="resetForm(ruleFormRef)">重置</el-button>
          </el-form-item>
        </td>
      </tr>
    </table>






  </el-form>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from "vue";
import { ElMessage, type ComponentSize, type FormInstance, type FormRules } from "element-plus";
import axios from "axios";
import {useRoute, useRouter} from 'vue-router'

const apiUrl = import.meta.env.VITE_APP_API_URL;
const apiBedinUrl = import.meta.env.VITE_APPRead_API_URL;

//创建路由响应实例
const route=useRoute()
const router=useRouter()

const formSize = ref<ComponentSize>("default");
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive({
  prenatalCheckupId: 0,
  documentNumber: "",
  serialNumber: "",
  basicInforId: '',
  pregnancyTestResultId: "",
  pregnancyTestTypeId: '',
  parityNumber: '',
  currentBuilding: "",
  currentfield: "",
  turnBuilding: '',
  turnColumn: '',
  principal: '',
  pregnancyTestDate: '',
  isPrenatalCheckUp: 1,
  createdOwner: '',
  createdTime: '',
  updateOwner:localStorage.getItem("userMyName"),
  updatedTime:'',
  examineOwner: '',
  examineTime: null,
  earNumber:''
});

const locationOptions = ["Home", "Company", "School"];

const rules = reactive({
  name: [{ required: true, message: "Please input Activity name", trigger: "blur" }]
});

//反填
const handlePrenatalDetail=()=>{
  axios.get(apiBedinUrl+'/api/PrenatalCheckup/DetailPrenatalCheckup',{
    params:{
      PrenatalCheckupId:route.query.obj
    }
  }).then(res=>{
  //反填
  console.log(res.data.data)
  ruleForm.prenatalCheckupId=res.data.data.prenatalCheckupId;//编号
  ruleForm.documentNumber=res.data.data.documentNumber;//单据号
  ruleForm.serialNumber=res.data.data.serialNumber;//流水号
  ruleForm.basicInforId=res.data.data.basicInforId;//基本信息外键
  ruleForm.pregnancyTestResultId=res.data.data.pregnancyTestResultId;//妊检结果
  ruleForm.pregnancyTestTypeId=res.data.data.pregnancyTestTypeId;//妊检方式
  ruleForm.parityNumber=res.data.data.parityNumber;//同胎数
  ruleForm.currentBuilding=res.data.data.currentBuilding;//当前栋舍
  ruleForm.currentfield=res.data.data.currentfield;//当前栏位
  ruleForm.turnBuilding=res.data.data.turnBuilding;//转入栋舍
  //二级联动
  handlefield()
  ruleForm.turnColumn=res.data.data.turnColumn;//转入栏位
  ruleForm.principal=res.data.data.principal;//负责人
  ruleForm.pregnancyTestDate=res.data.data.pregnancyTestDate;//妊检时间
  ruleForm.isPrenatalCheckUp=res.data.data.isPrenatalCheckUp;
  ruleForm.createdOwner=res.data.data.createdOwner;
  ruleForm.createdTime=res.data.data.createdTime;
  ruleForm.examineOwner=res.data.data.examineOwner;
  ruleForm.examineTime=res.data.data.examineTime;
  //耳号
  ruleForm.earNumber=res.data.data.earNumber;
  })
}



//修改保存
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      axios.put(apiBedinUrl+'/api/PrenatalCheckup/UpdatePrenatalCheckup',ruleForm)
      .then(res=>{
        console.log(res.data.data)
        if(res.data.code==0){
          ElMessage.success("修改成功");
          router.push('/PrenatalCheckupsList')
        }else{
          ElMessage.error("修改失败");
        }
      })
    } else {
      console.log("error submit!", fields);
    }
  });
};
//重置
const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

//--------------下拉框---------------//

//获取负责人
const userList=ref([])
const handleprincipal=()=>{
    axios.post(apiUrl+'/api/Users/GetUsers',{
        pageIndex:1,
        pageSize:100,
    })
    .then(res=>{
        userList.value=res.data.data.list
    })
}

//获取妊检结果
const pregnancyResultList:any=ref([])
const handlePregnancyResult=()=>{
    axios.get(apiBedinUrl+'/api/PrenatalCheckup/SearchPregnancyResult',{})
    .then(res=>{
        pregnancyResultList.value=res.data.data
    })
}

//获取妊检方式
const pregnancyTypeList:any=ref([])
const handlePregnancyType=()=>{
    axios.get(apiBedinUrl+'/api/PrenatalCheckup/SearchPregnancyType',{})
    .then(res=>{
        pregnancyTypeList.value=res.data.data
    })
}

//获取栋舍
const bedroomList:any=ref([])
const handleBedroom=()=>{
    axios.post(apiUrl+'/api/BedRoom/ShowBedroom',{
        pageIndex: 1,
        pageSize: 1000,
        bedroomTypeId: 0,
        bedroomName: '',
        personName:''
    }).then(res=>{
        bedroomList.value=res.data.data.list
    })
}

//获取栏位
const fieldList:any=ref([])
const handlefield=()=>{
    ruleForm.turnColumn=''
    axios.post(apiUrl+'/api/BedRoom/GetBrdroomIdField',{
        bedroomId:ruleForm.turnBuilding
    }).then(res=>{
        fieldList.value=res.data.data
    })
}

//创建生命周期函数钩
onMounted(()=>{
  handlePrenatalDetail();
  handleprincipal();//负责人
  handlePregnancyResult();//妊检结果
  handlePregnancyType();//妊检方式
  handleBedroom();//栋舍
})
</script>
